#!/usr/bin/env bash
usage()
{
       echo "Usage: ${0##*/} {start|stop|restart} "
       exit 1
}

[ $# -gt 0 ] || usage

# config by SA setup
PROJECT_NAME=dbdump
PROJECT_HOME=/home/admin/$PROJECT_NAME
JETTY_HOME=/home/admin/$PROJECT_NAME/.default
STDOUT_LOG=$PROJECT_HOME/logs/jetty_stdout.log
OPT_JETTY=/home/admin/jetty7.3.1

JAVA_OPTIONS="-server -Xms5g -Xmx5g -XX:NewSize=320m -XX:MaxNewSize=320m -XX:PermSize=128m -XX:MaxPermSize=256m -Xmn2g"
#JAVA_OPTIONS="-server -Xms2g -Xmx2g -XX:NewSize=320m -XX:MaxNewSize=320m -XX:PermSize=128m -XX:MaxPermSize=256m -Xmn600m"
JAVA_OPTIONS="$JAVA_OPTIONS -verbose:gc -Xloggc:/home/admin/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow  -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+CMSParallelRemarkEnabled  -XX:+CMSClassUnloadingEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 -XX:+DisableExplicitGC -XX:SurvivorRatio=10"

JAVA_OPTIONS="$JAVA_OPTIONS -Djava.awt.headless=true"
JAVA_OPTIONS="$JAVA_OPTIONS -Dsun.net.client.defaultConnectTimeout=10000"
JAVA_OPTIONS="$JAVA_OPTIONS -Dsun.net.client.defaultReadTimeout=30000"
JAVA_OPTIONS="$JAVA_OPTIONS -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"

# taobao common
export ORACLE_HOME=/home/admin/oracle
#export JAVA_HOME=/opt/taobao/java
export LD_LIBRARY_PATH=/opt/taobao/oracle/lib:$PROJECT_HOME/lib:$LD_LIBRARY_PATH
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK

#var for copy
#ETC_PATH=$PROJECT_HOME/etc
#CONTEXTS_PATH=$PROJECT_HOME/contexts

#var for start/stop
JETTY_PS_STR="-Djetty.home"

start()
{
       local PID=`ps -C java -f --width 2000 | grep -- "$JETTY_PS_STR" | grep -- "-Dproject.home=$PROJECT_HOME" | awk '{print $2}'`
       if [ $PID ]; then
               echo "Already Running!"
               exit 1
       fi

       mv  /home/admin/logs/gc.log /home/admin/logs/gc.log.`date +%Y%m%d%H%M%S`
       if [ -d "$JETTY_HOME" ]; then
               rm -rf $JETTY_HOME
       fi
       echo -ne "Copy .default Dir : "
       mkdir -p $JETTY_HOME
       cp -af $OPT_JETTY/* $JETTY_HOME/.
       echo OK

       #Extract tgz File
       if [ -d "$PROJECT_HOME/target/${PROJECT_NAME}.war" ]; then
               if [ -f "$PROJECT_HOME/target/${PROJECT_NAME}.tar.gz" ]; then
                       tgz_time=`stat -c '%Y' $PROJECT_HOME/target/${PROJECT_NAME}.tar.gz`
                       ear_time=`stat -c '%Y' $PROJECT_HOME/target/${PROJECT_NAME}.war`
                       if [ $tgz_time -gt $ear_time ]; then
                               echo -ne "Extract $PROJECT_HOME/target/${PROJECT_NAME}.tar.gz : "
                               rm -rf "$PROJECT_HOME/target/${PROJECT_NAME}.war"
                               cd $PROJECT_HOME/target
                               tar zxf $PROJECT_HOME/target/${PROJECT_NAME}.tar.gz
                               echo OK
                       fi
               else
                       echo "$PROJECT_HOME/target/${PROJECT_NAME}.tar.gz file not exist"
               fi
       else
               if [ -f "$PROJECT_HOME/target/${PROJECT_NAME}.tar.gz" ]; then
                       echo -ne "Extract $PROJECT_HOME/target/${PROJECT_NAME}.tar.gz : "
                       cd $PROJECT_HOME/target
                       tar zxf $PROJECT_HOME/target/${PROJECT_NAME}.tar.gz
                       echo OK
               else
                       echo "$PROJECT_HOME/target/${PROJECT_NAME}.tar.gz file not exist"
               fi
       fi

       if [ -d "$PROJECT_HOME/target/taobao-hsf.sar" ]; then
               if [ -f "$PROJECT_HOME/target/taobao-hsf.tgz" ]; then
                       hsf_tgz_time=`stat -c '%Y' $PROJECT_HOME/target/taobao-hsf.tgz`
                       hsf_ear_time=`stat -c '%Y' $PROJECT_HOME/target/taobao-hsf.sar`
                       if [ $hsf_tgz_time -gt $hsf_ear_time ]; then
                               echo -ne "Extract $PROJECT_HOME/target/taobao-hsf.tgz : "
                               rm -rf "$PROJECT_HOME/target/taobao-hsf.sar"
                               cd $PROJECT_HOME/target
                               tar zxf $PROJECT_HOME/target/taobao-hsf.tgz
                               echo OK
                       fi
               else
                       echo "$PROJECT_HOME/target/taobao-hsf.tgz file not exist"
               fi
       else
               if [ -f "$PROJECT_HOME/target/taobao-hsf.tgz" ]; then
                       echo -ne "Extract $PROJECT_HOME/target/taobao-hsf.tgz"
                       cd $PROJECT_HOME/target
                       tar zxf $PROJECT_HOME/target/taobao-hsf.tgz
                       echo OK
               else
                       echo "$PROJECT_HOME/target/taobao-hsf.tgz file not exist"
               fi
       fi

       cd $JETTY_HOME/target/
       ln -s $PROJECT_HOME/target/taobao-hsf.sar .
       ln -s $PROJECT_HOME/target/${PROJECT_NAME}.war .

       cd $JETTY_HOME
       rm -f $STDOUT_LOG
       touch $STDOUT_LOG
       nohup $JAVA_HOME/bin/java $JAVA_OPTIONS -Djetty.home=$JETTY_HOME -Dproject.name=$PROJECT_NAME -Dproject.home=$PROJECT_HOME -jar $JETTY_HOME/start.jar --ini=$JETTY_HOME/start.ini --daemon > /dev/null &

       disown $!
       echo "`date` Jetty $! Started !"
}

stop()
{
       local PID=`ps -C java -f --width 2000 | grep -- "$JETTY_PS_STR" | grep -- "-Dproject.home=$PROJECT_HOME" | awk '{print $2}'`

       echo -ne "Stopping Jetty: "
       if [ $PID ]; then
               echo -ne "$PID "
               TIMEOUT=3
               while [ $TIMEOUT -gt 0 ]; do
                       kill -9 "$PID" 2>/dev/null
                       TIMEOUT=$(($TIMEOUT-1))
                       sleep 1
               done
       fi
       echo OK

       echo -ne "Remove .default Dir: "
       rm -rf $PROJECT_HOME/.default
       echo OK
}

ACTION=$1
case "$ACTION" in
       start)
               start
       ;;

       stop)
               stop
       ;;

       restart)
               stop
               start
       ;;

       deploy)
               stop
               start
       ;;

       *)
               usage
       ;;
esac

exit 0